#include "SceneManager.h"
#include "snake_scene.h"
#include <graphics.h>
#include <chrono>
#include <thread>

int main() {
    const int screenWidth = 800;
    const int screenHeight = 600;
    const int gridSize = 20;

    initgraph(screenWidth, screenHeight);
    setbkcolor(WHITE);
    cleardevice();

    GameEngine::SceneManager sceneManager;
    sceneManager.RegisterScene<SnakeScene>("SnakeScene", screenWidth, screenHeight, gridSize);
    sceneManager.SwitchToScene("SnakeScene");

    bool running = true;
    auto lastTime = std::chrono::high_resolution_clock::now();

    while (running) {
        auto now = std::chrono::high_resolution_clock::now();
        float deltaTime = std::chrono::duration<float>(now - lastTime).count();
        lastTime = now;

        // 只处理关闭消息
        ExMessage msg;
        if (peekmessage(&msg, EX_MOUSE | EX_KEY, false) && msg.message == WM_CLOSE) {
            getmessage(&msg, EX_KEY); // 移除关闭消息
            running = false;
        }

        sceneManager.UpdateCurrentScene(deltaTime);
        sceneManager.RenderCurrentScene();

        // 控制帧率
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    closegraph();
    return 0;
}